package com.work.music.service;

import com.work.music.common.HttpRequestParamter;
import com.work.music.common.ResponseJson;
import com.work.music.dao.*;
import com.work.music.entity.*;
import com.work.music.exception.AjaxOperationFailException;
import com.work.music.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by xs on 2019/4/16.
 */
@Service
@Transactional
public class MusicManagerService extends BaseService {
    @Autowired
    private MusicManagerMapper musicManagerMapper;

    @Autowired
    private MusicSongsterMapper musicSongsterMapper;

    @Autowired
    private MusicAlbumMapper musicAlbumMapper;

    @Autowired
    private MusicLyricsMapper musicLyricsMapper;

    @Autowired
    private MusicUserMapper musicUserMapper;

    @Autowired
    private IntegralTypeMapper integralTypeMapper;

    /**
     * 获取歌手列表信息
     * @return
     */
    public ResponseJson ajaxSongsterList() {
        String name = HttpRequestParamter.getString("name");
        int status = HttpRequestParamter.getInt("status", -1);
        int page = HttpRequestParamter.getInt("page", 1);
        int offset = HttpRequestParamter.getInt("limit", 20);

        StringBuffer stringBuffer = new StringBuffer("where 1=1 ");
        if(!StringUtil.isEmpty(name)) {
            stringBuffer.append(" and name like '%").append(name).append("%'");
        }
        if(status != -1) {
            stringBuffer.append(" and status=").append(status);
        }

        int count = musicManagerMapper.selectSongsterCount(stringBuffer.toString());
        if(count <= 0) {
            return new ResponseJson();
        }
        List<MusicSongsterManager> list = musicManagerMapper.selectSongsterList(stringBuffer.toString(), (page - 1) * offset, offset);
        if(list == null || list.size() <= 0) {
            list = new ArrayList<MusicSongsterManager>();
        } else {
            for (MusicSongsterManager musicSongsterManager: list) {
                musicSongsterManager.setHeadimg(StringUtil.getImg(musicSongsterManager.getHeadimg()));
            }
        }
        ResponseJson responseJson = new ResponseJson();
        responseJson.setList(list);
        responseJson.setNum(count);
        return responseJson;
    }

    public MusicSongsterManager getSongsterById(long id) {
        if(id == -1) {
            return new MusicSongsterManager();
        }
        MusicSongsterManager musicSongsterManager = musicManagerMapper.getMusicSongsterById(id);
        if(musicSongsterManager == null) {
            return new MusicSongsterManager();
        }
        musicSongsterManager.setHeadimg(StringUtil.getImg(musicSongsterManager.getHeadimg()));
        return musicSongsterManager;
    }

    /**
     * 审核歌手信息
     * @return
     */
    public ResponseJson ajaxSongsterEdit() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id", -1);
        int status = HttpRequestParamter.getInt("status", -1);
        String reason = HttpRequestParamter.getString("reason");
        if(id == -1) {
            throw new AjaxOperationFailException("请选择歌手信息");
        }
        if(status != 1 && status != 2 && status != 0) {
            throw new AjaxOperationFailException("请选择状态");
        }
        MusicSongster musicSongster = musicSongsterMapper.selectByPrimaryKey(id);
        if(musicSongster == null) {
            throw new AjaxOperationFailException("该歌手信息不存在");
        }
        musicSongster.setStatus(status);
        if(!StringUtil.isNull(reason)) {
            musicSongster.setReason(reason);
        }
        int tag = musicSongsterMapper.updateByPrimaryKey(musicSongster);
        if(tag <= 0) {
            throw new AjaxOperationFailException("处理失败");
        }
        return new ResponseJson();
    }

    public MusicAlbumManager getAlbumById(long id) {
        if(id == -1) {
            return new MusicAlbumManager();
        }
        MusicAlbumManager musicAlbumManager = musicManagerMapper.getAlbumById(id);
        if(musicAlbumManager == null) {
            return new MusicAlbumManager();
        }
        musicAlbumManager.setCover(StringUtil.getImg(musicAlbumManager.getCover()));
        return musicAlbumManager;
    }

    public ResponseJson ajaxAlbumList() {
        String name = HttpRequestParamter.getString("name");
        int status = HttpRequestParamter.getInt("status", -1);
        int page = HttpRequestParamter.getInt("page", 1);
        int offset = HttpRequestParamter.getInt("limit", 20);

        StringBuffer stringBuffer = new StringBuffer("where 1=1 ");
        if(!StringUtil.isEmpty(name)) {
            stringBuffer.append(" and name like '%").append(name).append("%'");
        }
        if(status != -1) {
            stringBuffer.append(" and status=").append(status);
        }

        int count = musicManagerMapper.selectAlbumCount(stringBuffer.toString());
        if(count <= 0) {
            return new ResponseJson();
        }
        List<MusicAlbumManager> list = musicManagerMapper.selectAlbumList(stringBuffer.toString(), (page - 1) * offset, offset);
        if(list == null || list.size() <= 0) {
            list = new ArrayList<MusicAlbumManager>();
        } else {
            for (MusicAlbumManager musicAlbumManager: list) {
                musicAlbumManager.setCover(StringUtil.getImg(musicAlbumManager.getCover()));
            }
        }
        ResponseJson responseJson = new ResponseJson();
        responseJson.setList(list);
        responseJson.setNum(count);
        return responseJson;
    }

    /**
     * 修改专辑信息
     * @return
     */
    public ResponseJson ajaxAlbumEdit() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id", -1);
        int status = HttpRequestParamter.getInt("status", -1);
        String reason = HttpRequestParamter.getString("reason");
        if(id == -1) {
            throw new AjaxOperationFailException("请选择专辑信息");
        }
        if(status != 1 && status != 2 && status != 0) {
            throw new AjaxOperationFailException("请选择状态");
        }
        MusicAlbum musicAlbum = musicAlbumMapper.selectByPrimaryKey(id);
        if(musicAlbum == null) {
            throw new AjaxOperationFailException("该专辑信息不存在");
        }
        musicAlbum.setStatus(status);
        if(!StringUtil.isEmpty(reason)) {
            musicAlbum.setReason(reason);
        }
        int tag = musicAlbumMapper.updateByPrimaryKey(musicAlbum);
        if(tag <= 0) {
            throw new AjaxOperationFailException("修改失败");
        }
        return new ResponseJson();
    }

    public MusicLyricsManager getLyricsById(long id) {
        if(id == -1) {
            return new MusicLyricsManager();
        }
        MusicLyricsManager musicLyricsManager = musicManagerMapper.getLyricsById(id);
        if(musicLyricsManager == null) {
            return new MusicLyricsManager();
        }
        return musicLyricsManager;
    }

    /**
     * 获取歌词列表信息
     * @return
     */
    public ResponseJson ajaxLyricsList() {
        String name = HttpRequestParamter.getString("name");
        int status = HttpRequestParamter.getInt("status", -1);
        int page = HttpRequestParamter.getInt("page", 1);
        int offset = HttpRequestParamter.getInt("limit", 20);

        StringBuffer stringBuffer = new StringBuffer("where 1=1 ");
        if(!StringUtil.isEmpty(name)) {
            stringBuffer.append(" and name like '%").append(name).append("%'");
        }
        if(status != -1) {
            stringBuffer.append(" and status=").append(status);
        }

        int count = musicManagerMapper.selectLyricsCount(stringBuffer.toString());
        if(count <= 0) {
            return new ResponseJson();
        }
        List<MusicLyricsManager> list = musicManagerMapper.selectLyricsList(stringBuffer.toString(), (page - 1) * offset, offset);
        if(list == null || list.size() <= 0) {
            list = new ArrayList<MusicLyricsManager>();
        } else {
            for (MusicLyricsManager musicLyricsManager: list) {
                if(musicLyricsManager.getType() == 0) {
                    MusicAlbum musicAlbum = musicLyricsManager.getMusicAlbum();
                    musicAlbum.setCover(StringUtil.getImg(musicAlbum.getCover()));
                    musicLyricsManager.setMusicAlbum(musicAlbum);
                } else {
                    musicLyricsManager.setCover(StringUtil.getImg(musicLyricsManager.getCover()));
                }
            }
        }
        ResponseJson responseJson = new ResponseJson();
        responseJson.setList(list);
        responseJson.setNum(count);
        return responseJson;
    }

    /**
     * 编辑歌词信息
     * @return
     */
    public ResponseJson ajaxLyricsEdit() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id", -1);
        int status = HttpRequestParamter.getInt("status", -1);
        String reason = HttpRequestParamter.getString("reason");
        if(id == -1) {
            throw new AjaxOperationFailException("请选择专辑信息");
        }
        if(status != 1 && status != 2 && status != 0) {
            throw new AjaxOperationFailException("请选择状态");
        }
        MusicLyrics musicLyrics = musicLyricsMapper.selectByPrimaryKey(id);
        if(musicLyrics == null) {
            throw new AjaxOperationFailException("该专辑信息不存在");
        }
        musicLyrics.setStatus(status);
        if(!StringUtil.isEmpty(reason)) {
            musicLyrics.setReason(reason);
        }
        int tag = musicLyricsMapper.updateByPrimaryKey(musicLyrics);
        if(tag <= 0) {
            throw new AjaxOperationFailException("修改失败");
        }
        return new ResponseJson();
    }

    public ResponseJson ajaxUserList() {
        String name = HttpRequestParamter.getString("name");
        int status = HttpRequestParamter.getInt("status", -1);
        int page = HttpRequestParamter.getInt("page", 1);
        int offset = HttpRequestParamter.getInt("limit", 20);

        StringBuffer stringBuffer = new StringBuffer("where 1=1 ");
        if(!StringUtil.isEmpty(name)) {
            stringBuffer.append(" and user_name like '%").append(name).append("%'");
        }
        if(status != -1) {
            stringBuffer.append(" and state=").append(status);
        }

        int count = musicUserMapper.selectLyricsCount(stringBuffer.toString());
        if(count <= 0) {
            return new ResponseJson();
        }
        List<MusicUser> list = musicUserMapper.selectLyricsList(stringBuffer.toString(), (page - 1) * offset, offset);
        if(list == null || list.size() <= 0) {
            list = new ArrayList<MusicUser>();
        } else {
            for (MusicUser musicUser: list) {
                musicUser.setHeadimg(StringUtil.getImg(musicUser.getHeadimg()));
            }
        }
        ResponseJson responseJson = new ResponseJson();
        responseJson.setList(list);
        responseJson.setNum(count);
        return responseJson;
    }

    /**
     * 修改用户信息
     * @return
     */
    public ResponseJson ajaxInfoEdit() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id");
        int state = HttpRequestParamter.getInt("state");
        if (id == 0) {
            throw new AjaxOperationFailException("用户id不能为空");
        }
        if (state != 0 && state != 1) {
            throw new AjaxOperationFailException("请选择正确的修改状态");
        }
        MusicUser musicUser = musicUserMapper.selectByPrimaryKey(id);
        if (musicUser == null) {
            throw new AjaxOperationFailException("需要修改的用户信息不存在");
        }
        if (musicUser.getState() == state) {
            musicUser.setState(state == 0 ? 1 : 0);
            musicUserMapper.updateByPrimaryKey(musicUser);
        }
        return new ResponseJson();
    }

    public ResponseJson ajaxIntegralTypeList() {
        int type = HttpRequestParamter.getInt("type");
        int page = HttpRequestParamter.getInt("page", 1);
        int offset = HttpRequestParamter.getInt("limit", 20);

        StringBuffer stringBuffer = new StringBuffer("where 1=1 ");
        if(type != -1 && type != 0) {
            stringBuffer.append(" and type =").append(type - 1);
        }

        int count = integralTypeMapper.selectIntegralTypeCount(stringBuffer.toString());
        if(count <= 0) {
            return new ResponseJson();
        }
        List<IntegralType> list = integralTypeMapper.selectIntegralTypeList(stringBuffer.toString(), (page - 1) * offset, offset);
        if(list == null || list.size() <= 0) {
            list = new ArrayList<IntegralType>();
        }
        ResponseJson responseJson = new ResponseJson();
        responseJson.setList(list);
        responseJson.setNum(count);
        return responseJson;
    }

    /**
     * 返回积分类型信息
     * @return
     */
    public IntegralType integraltypeById() {
        long id = HttpRequestParamter.getLong("id");
        if (id == 0) {
            return new IntegralType();
        }
        IntegralType integralType = integralTypeMapper.selectByPrimaryKey(id);
        if (integralType == null) {
            integralType = new IntegralType();
        }
        return integralType;
    }

    public ResponseJson ajaxIntegralTypeEdit() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id");
        String desc = HttpRequestParamter.getString("desc");
        long integral = HttpRequestParamter.getLong("integral");
        if (StringUtil.isNull(desc)) {
            throw new AjaxOperationFailException("简介不能为空");
        }
        if (integral <= 0) {
            throw new AjaxOperationFailException("积分不能小于0");
        }
        IntegralType integralType;
        if (id != 0) {
            integralType = integralTypeMapper.selectByPrimaryKey(id);
            if (integralType == null) {
                throw new AjaxOperationFailException("该积分类型不存在");
            }
            integralType.setDesc(desc);
            integralType.setIntegral(integral);
            integralTypeMapper.updateByPrimaryKey(integralType);
        } else {
            integralType = new IntegralType();
            integralType.setType(1);
            integralType.setDesc(desc);
            integralType.setIntegral(integral);
            integralTypeMapper.insert(integralType);
        }
        return new ResponseJson();
    }

    /**
     * 删除积分类型
     * @return
     */
    public ResponseJson ajaxintegraltypeDel() throws AjaxOperationFailException {
        long id = HttpRequestParamter.getLong("id");
        if (id == 0) {
            throw new AjaxOperationFailException("id不能为空");
        }
        IntegralType integralType = integralTypeMapper.selectByPrimaryKey(id);
        if (integralType == null) {
            throw new AjaxOperationFailException("该积分类型不存在");
        }
        integralTypeMapper.deleteByPrimaryKey(id);
        return new ResponseJson();
    }
}
